package www.wen.com.common.lib.base.activity

import android.os.Bundle
import androidx.databinding.ViewDataBinding
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.CenterPopupView
import com.lxj.xpopup.impl.LoadingPopupView
import www.wen.com.common.lib.base.module.BaseViewModel
import www.wen.com.common.lib.data.entities.LoadingDialog
import www.wen.com.common.lib.data.entities.LoadingDialogStateType
import www.wen.com.common.lib.data.entities.StateType
import www.wen.com.common.lib.data.entities.UiStateCustom
import www.wen.com.common.lib.extension.toast


/**
 * @author WEN
 * @Description: 这是一个通用加载框的Activity，由viewModel通过livedata来发起,如果页面需要发起请求弹出标准加载框的请继承这个基类
 * @date 2020/11/1 21:54
 */
abstract class BaseLoadingActivity<K : ViewDataBinding> : BaseActivity<K>() {

    abstract val viewModel: BaseViewModel

    private var loadingPopup: LoadingPopupView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        addObserve()
    }

    open fun addObserve() {
        viewModel.run {
            viewModel.loading.observe(this@BaseLoadingActivity, {
                if (it.stateType == StateType.LOADING_DIALOG) {
                    val data = it.stateData as LoadingDialog<String>
                    when (data.loadingDialogStateType) {
                        LoadingDialogStateType.LOADING -> {
                            showLoadingView(data.tip)
                        }
                        LoadingDialogStateType.SUCCESS -> {
                            if (it.stateData.isShowDialogTip) {
                                loadingPopup!!.postDelayed({ if (it.stateData.isShowDialogTip) { loadingPopup!!.setTitle(data.tip) } }, 300)
                                dismissLoadingView(1500, runnable = Runnable {
                                    loading.value = UiStateCustom(stateData = LoadingDialog(data.tip, LoadingDialogStateType.DISMISS, data.isShowDialogTip, data.isShowToastTip, data.action))
                                })
                            } else if (it.stateData.isShowToastTip) {
                                dismissLoadingView(300, runnable = Runnable {
                                    loading.value = UiStateCustom(stateData = LoadingDialog(data.tip, LoadingDialogStateType.DISMISS, data.isShowDialogTip, data.isShowToastTip, data.action))
                                })
                            }else{
                                loading.value = UiStateCustom(stateData = LoadingDialog(data.tip, LoadingDialogStateType.DISMISS, data.isShowDialogTip, data.isShowToastTip, data.action))
                            }
                        }
                        LoadingDialogStateType.ERROR -> {
                            if (it.stateData.isShowDialogTip) {
                                loadingPopup!!.postDelayed({
                                    loadingPopup!!.setTitle(data.tip)
                                }, 300)
                            }
                            dismissLoadingView(1500, runnable = Runnable {
                                loading.value = UiStateCustom(stateData = LoadingDialog(data.tip, LoadingDialogStateType.DISMISS, data.isShowDialogTip, data.isShowToastTip, data.action))
                            })
                        }
                        LoadingDialogStateType.DISMISS -> {
                            dismissLoadingView(0)
                            if (it.stateData.isShowToastTip) {
                                toast(data.tip)
                            }
                            it.stateData.action?.invoke()
                        }
                    }
                }
            })
        }
    }

    open fun showLoadingView(loadingText: String): CenterPopupView? {
        if (loadingPopup == null) {
            loadingPopup = XPopup
                .Builder(this)
                .dismissOnTouchOutside(false)
                .dismissOnBackPressed(false)
                .asLoading(loadingText)
                .show() as LoadingPopupView
        }
        loadingPopup?.setTitle(loadingText)
        loadingPopup?.show()
        return loadingPopup
    }


    open fun dismissLoadingView(delay: Long, runnable: Runnable = Runnable {}) {
        loadingPopup?.run {
            loadingPopup!!.delayDismissWith(delay, runnable)
        }
    }

}

